package com.exam.Controller;


import com.exam.Entity.ApiResult;
import com.exam.Entity.ExamManage;
import com.exam.Service.ExamManageService;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
public class ExamManageController {

    @Autowired
    private ExamManageService examManageService;

    @GetMapping("/exams")
    public ApiResult findAll(){
        System.out.println("不分页查询所有试卷");
        ApiResult apiResult;
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", examManageService.findAll());
        return apiResult;
    }

@GetMapping("/exams/{page}/{size}")
public ApiResult findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
    System.out.println("分页查询所有试卷");

    // 获取试卷列表
    List<ExamManage> res = examManageService.findAll(page, size);
    List<ExamManage>  t = examManageService.findTotal(page, size);
    System.out.println("找到的试卷" + res);

    // 构造返回数据
    Map<String, Object> data = new java.util.HashMap<>();
    data.put("records", res);               // 返回的试卷列表
    data.put("current", page);              // 当前页码
    data.put("size", size);                 // 每页大小
    data.put("total", t.size());          // 总记录数（列表长度）

    // 构建 ApiResult
    ApiResult apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", data);

    return apiResult;
}


    @GetMapping("/exam/{examCode}")
    public ApiResult findById(@PathVariable("examCode") Integer examCode){
        System.out.println("根据ID查找");
        ExamManage res = examManageService.findById(examCode);
        if(res == null) {
            return ApiResultHandler.buildApiResult(10000,"考试编号不存在",null);
        }
        return ApiResultHandler.buildApiResult(200,"请求成功！",res);
    }

    @DeleteMapping("/exam/{examCode}")
    public ApiResult deleteById(@PathVariable("examCode") Integer examCode){
        int res = examManageService.delete(examCode);
        return ApiResultHandler.buildApiResult(200,"删除成功",res);
    }

    @PutMapping("/exam")
    public ApiResult update(@RequestBody ExamManage exammanage){
        int res = examManageService.update(exammanage);
//        if (res == 0) {
//            return ApiResultHandler.buildApiResult(20000,"请求参数错误");
//        }
        System.out.print("更新操作执行---");
        return ApiResultHandler.buildApiResult(200,"更新成功",res);
    }

    @PostMapping("/exam")
    public ApiResult add(@RequestBody ExamManage exammanage){
        int res = examManageService.add(exammanage);
        if (res ==1) {
            return ApiResultHandler.buildApiResult(200, "添加成功", res);
        } else {
            return  ApiResultHandler.buildApiResult(400,"添加失败",res);
        }
    }

    @GetMapping("/examManagePaperId")
    public ApiResult findOnlyPaperId() {
        ExamManage res = examManageService.findOnlyPaperId();
        if (res != null) {
            return ApiResultHandler.buildApiResult(200,"请求成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"请求失败",res);
    }
}
